home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 001 / pibt40s2.arc / LTRIM.MOD < prev    next >
Text File  |  1987-05-18  |  6KB  |  82 lines

  1. (*--------------------------------------------------------------------------*)
  2. (*               LTrim --- LTrim leading blanks from a string               *)
  3. (*--------------------------------------------------------------------------*)
  4.  
  5. FUNCTION LTrim( S : AnyStr ) : AnyStr;
  6.  
  7. (*--------------------------------------------------------------------------*)
  8. (*                                                                          *)
  9. (*     Function:   LTrim                                                    *)
  10. (*                                                                          *)
  11. (*     Purpose:    Trims leading blanks from a string                       *)
  12. (*                                                                          *)
  13. (*     Calling sequence:                                                    *)
  14. (*                                                                          *)
  15. (*         LTrimmed_S := TRIM( S );                                         *)
  16. (*                                                                          *)
  17. (*            S           --- the string to be trimmed                      *)
  18. (*            LTrimmed_S  --- the trimmed version of S                      *)
  19. (*                                                                          *)
  20. (*     Calls:  None                                                         *)
  21. (*                                                                          *)
  22. (*     Remarks:                                                             *)
  23. (*                                                                          *)
  24. (*        Note that the original string itself is left untrimmed.           *)
  25. (*                                                                          *)
  26. (*     A Pascal version of this routine could be written as:                *)
  27. (*                                                                          *)
  28. (*        VAR                                                               *)
  29. (*           I:       INTEGER;                                              *)
  30. (*           L:       INTEGER;                                              *)
  31. (*                                                                          *)
  32. (*        BEGIN                                                             *)
  33. (*                                                                          *)
  34. (*           I := 1;                                                        *)
  35. (*           L := ORD( S[0] );                                              *)
  36. (*                                                                          *)
  37. (*           WHILE ( ( I <= L ) AND ( S[I] = ' ' ) ) DO                     *)
  38. (*              I := SUCC( I );                                             *)
  39. (*                                                                          *)
  40. (*           S[0]  := CHR( MAX( L - I + 1 , 0 ) );                          *)
  41. (*                                                                          *)
  42. (*           MOVE( S[I], S[1], ORD( S[0] ) );                               *)
  43. (*                                                                          *)
  44. (*           LTrim := S;                                                    *)
  45. (*                                                                          *)
  46. (*        END;                                                              *)
  47. (*                                                                          *)
  48. (*--------------------------------------------------------------------------*)
  49.  
  50. BEGIN (* LTrim *)
  51.  
  52. INLINE(
  53.   $1E/                   {         PUSH    DS                ; Save DS}
  54.                          {;}
  55.   $8D/$BE/>S+1/          {         LEA     DI,[BP+>S+1]      ; Offset of first source character}
  56.   $8A/$8E/>S/            {         MOV     CL,[BP+>S]        ; Length of S}
  57.   $30/$ED/               {         XOR     CH,CH}
  58.   $E3/$1F/               {         JCXZ    LTrim2            ; If null string, no trimming}
  59.   $8C/$D0/               {         MOV     AX,SS             ; Get stack segment ...}
  60.   $8E/$D8/               {         MOV     DS,AX             ; for addressing source ...}
  61.   $8E/$C0/               {         MOV     ES,AX             ; and destination strings}
  62.   $B0/$20/               {         MOV     AL,' '            ; Blank to AL}
  63.                          {;}
  64.   $FC/                   {         CLD                       ; Forward search}
  65.   $F3/$AE/               {         REPE    SCASB             ; Scan over blanks}
  66.   $74/$01/               {         JE      LTrim1            ; If CX=0, entire string is blank.}
  67.   $41/                   {         INC     CX}
  68.                          {;}
  69.   $88/$8E/$04/$01/       {LTrim1:  MOV     [BP+260],CL       ; Set length in result}
  70.   $89/$FE/               {         MOV     SI,DI             ; Offset of first non-blank}
  71.   $4E/                   {         DEC     SI}
  72.   $8D/$BE/$05/$01/       {         LEA     DI,[BP+261]       ; Offset of result}
  73.   $FC/                   {         CLD                       ; Forward move}
  74.   $F2/$A4/               {         REP     MOVSB             ; Move trimmed result}
  75.   $E9/$04/$00/           {         JMP     Exit}
  76.                          {;}
  77.   $88/$8E/$04/$01/       {LTrim2:  MOV     [BP+260],CL       ; Set length=0 in result}
  78.                          {;}
  79.   $1F);                  {Exit:    POP     DS                ; Restore DS}
  80.  
  81. END   (* LTrim *);
  82.